home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Apple Developer Connection Student Program
/
ADC Tools Sampler CD Disk 3 1999.iso
/
Documentation
/
Books
/
Learn Java on the Macintosh
/
Learn Java Projects
/
08.04 - next prime
/
NextPrime.java
< prev
next >
Wrap
Text File
|
1996-04-22
|
2KB
|
47 lines
/* -------------------------------------------------------------
This applet finds the next prime number after a starting point.
Java's classes: Applet (applet)
System (lang)
Math (lang)
Custom classes: NextPrime
------------------------------------------------------------- */
public class NextPrime extends java.applet.Applet {
public void init() {
int startingPoint, candidate, last, i;
boolean isPrime;
startingPoint = 19;
if ( startingPoint < 2 ) {
candidate = 2;
} else if ( startingPoint == 2 ) {
candidate = 3;
} else {
candidate = startingPoint;
if (candidate % 2 == 0) /* Test only odd numbers */
candidate--;
do {
isPrime = true; /* Assume glorious success */
candidate += 2; /* Bump to the next number to test */
last = (int)Math.sqrt( candidate ); /* We'll check to see if candidate */
/* has any factors, from 2 to last */
/* Loop through odd numbers only */
for ( i = 3; (i <= last) && isPrime; i += 2 ) {
if ( (candidate % i) == 0 )
isPrime = false;
}
} while ( ! isPrime );
}
System.out.println( "The next prime after " + startingPoint + " is " + candidate);
}
}